package com.xy.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xy.constants.ControlCharacter;
import com.xy.constants.RabbitConstant;
import com.xy.entity.ViewerEntity;
import com.xy.request.*;
import com.xy.response.InspectResponse;
import com.xy.response.PdaInittResponse;
import com.xy.service.IInspectBaseService;
import com.xy.service.IInspectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 核验相关类
 */
@RestController
@AllArgsConstructor
@RequestMapping("pda-inspect")
@Slf4j
@Api(value = "PDA核验管理", tags = "PDA核验管理")
public class PdaInspectController extends BladeController {
	private final IInspectService iInspectService;
	private final RabbitTemplate rabbitTemplate;
	private final IInspectBaseService iInspectBaseService;

	/**
	 * 身份证鉴权
	 *
	 * @param idCardRequest
	 * @return
	 */
	@PostMapping("/id-card")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "身份证鉴权", notes = "传入idCardDTO")
	public R<InspectResponse> idCard(@Valid @RequestBody IdCardRequest idCardRequest) {
		ViewerEntity viewerEntity = BeanUtil.copy(idCardRequest, ViewerEntity.class);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, idCardRequest, ControlCharacter.OAUTH_TYPE_ID_NUMBER);
		inspectResponse.setRecordViewer(idCardRequest.getIdCard());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_ID_NUMBER);
		saveRecordQueue(inspectResponse,idCardRequest);
		return R.data(inspectResponse);
	}
	/**
	 * 二维码识别
	 *
	 * @param twoCodeRequset
	 * @return
	 */
	@PostMapping("/two-code")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "二维码识别", notes = "传入 qrCodeDTO")
	public R<InspectResponse> qrCode(@Valid @RequestBody TwoCodeRequset twoCodeRequset) {
		ViewerEntity viewerEntity = BeanUtil.copy(twoCodeRequset, ViewerEntity.class);
		InspectResponse inspectResponse = iInspectService.oauthInspect(viewerEntity, twoCodeRequset, ControlCharacter.OAUTH_TYPE_CARD_NO);
		inspectResponse.setRecordViewer(twoCodeRequset.getTwoCode());
		inspectResponse.setOauthType(ControlCharacter.OAUTH_TYPE_CARD_NO);
		saveRecordQueue(inspectResponse,twoCodeRequset);
		return R.data(inspectResponse);
	}

	private void  saveRecordQueue(InspectResponse inspectResponse,BaseRequest baseRequest){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("inspectResponse",inspectResponse);
		jsonObject.put("baseRequest",baseRequest);
		rabbitTemplate.convertAndSend(RabbitConstant.TOPIC_RECORD_EXCHANGE,RabbitConstant.TOPIC_RECORD_ROUTING_KEY_ONE,jsonObject.toJSONString());
	}

	/**
	 * PDA初始化
	 *
	 * @param positionCode
	 * @return
	 */
	@GetMapping("/pad-init")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "二维码识别", notes = "传入 qrCodeDTO")
	public R<PdaInittResponse> padInit(String positionCode) {
		return R.data(iInspectBaseService.padInit(positionCode));
	}
}
